<template>


  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">


        <el-form-item label="网络端口">
          <el-input placeholder="请输入网络端口" v-model.trim="queryParam.ports" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="部署人">
          <el-input placeholder="请输入部署人" v-model.trim="queryParam.deployUser" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="工具版本">
          <el-input placeholder="请输入工具版本" v-model.trim="queryParam.toolVersion" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="工具名称">
          <el-input placeholder="请输入工具名称" v-model.trim="queryParam.toolName" clearable size="mini"></el-input>
        </el-form-item>


        <el-form-item>
          <el-button type="success" size="mini" @click="getListPage">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveBefore">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>





    <!--列表-->
    <el-card style="margin-top: 10px">

      <el-table
        :data="tableData"
        max-height="500px"
        height="500px"
        style="width: 100%">

        <el-table-column prop="name" label="名称" >
          <template #default="{row}">
            <div style="display: flex; align-items: center;">
              <el-image class="component-ico"  style="width: 40px; height: 40px; margin-right: 10px;" fit="contain" :src="(row.tool && row.tool.icoUrl) || require('@/assets/images/ico/tool.png')" />
              <span @click="openViewBefore(row)" style="cursor: pointer;">{{row.tool.name}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.tool.type === 1">项目管理</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.tool.type === 2">代码管理</el-tag>
            <el-tag size="mini" type="info" v-else-if="scope.row.tool.type === 9">其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="toolVersion" label="版本"  />
        <el-table-column prop="ports" label="端口"  />
        <el-table-column prop="tool.directorName" label="维护人"  />

        <el-table-column fixed="right" label="操作" width="170">
          <template slot-scope="scope">
            <el-button @click="openViewBefore(scope.row)" type="text" size="small"><i class="el-icon-view"></i>查看</el-button>
            <el-button @click="openUpdBefore(scope.row)" type="text" size="small"><i class="el-icon-edit"></i>编辑</el-button>
            <el-button @click="delTips(scope.row)" type="text" size="small"><i class="el-icon-delete"></i>删除</el-button>
          </template>
        </el-table-column>

      </el-table>








      <!--<div class="container">-->

      <!--  &lt;!&ndash;一个小框框&ndash;&gt;-->
      <!--  <div class="box" v-for="(item, index) in tableData" :key="index">-->
      <!--    &lt;!&ndash;<div class="env-type env-type-demo  component-title" v-if="item.opsDeploy.envType === 1">演示环境</div>&ndash;&gt;-->
      <!--    &lt;!&ndash;<div class="env-type env-type-dev  component-title" v-if="item.opsDeploy.envType === 2">开发环境</div>&ndash;&gt;-->

      <!--    <div class="box-content" >-->

      <!--      <el-row :gutter="10">-->
      <!--        <el-col :span="9">-->
      <!--          <div class="block">-->
      <!--            <el-image class="component-ico" v-if="item.tool.icoPath == null || item.tool.icoPath === '' || item.tool.icoPath === 'null'"-->
      <!--                      fit="contain" :src="require('@/assets/images/ico/tool.png')"/>-->
      <!--            <el-image v-else class="component-ico" fit="contain" :src="item.tool.icoPath"></el-image>-->
      <!--          </div>-->
      <!--        </el-col>-->
      <!--        <el-col :span="15">-->

      <!--          <el-row :gutter="10">-->
      <!--            <el-col :span="24">-->
      <!--              <h3 class="component-title"  @click="openViewBefore(item)">{{item.tool.name}}</h3>-->
      <!--            </el-col>-->
      <!--          </el-row>-->

      <!--          &lt;!&ndash;<el-row>&ndash;&gt;-->
      <!--          &lt;!&ndash;  <el-col :span="8">支持环境：</el-col>&ndash;&gt;-->
      <!--          &lt;!&ndash;  <el-col :span="16">&ndash;&gt;-->
      <!--          &lt;!&ndash;    <span>{{item.osStr}}</span>&ndash;&gt;-->
      <!--          &lt;!&ndash;  </el-col>&ndash;&gt;-->
      <!--          &lt;!&ndash;</el-row>&ndash;&gt;-->

      <!--          <el-row >-->
      <!--            <el-col :span="8">工具类型：</el-col>-->
      <!--            <el-col :span="16">-->
      <!--              <el-tag size="mini" v-if="item.tool.type === 1">项目管理</el-tag>-->
      <!--              <el-tag size="mini" type="success" v-else-if="item.tool.type === 2">代码管理</el-tag>-->
      <!--              <el-tag size="mini" type="info" v-else-if="item.tool.type === 9">其他</el-tag>-->
      <!--            </el-col>-->
      <!--          </el-row>-->

      <!--          <el-row style="margin-top: 5px">-->
      <!--            <el-col :span="8">版本：</el-col>-->
      <!--            <el-col :span="16">-->
      <!--              &lt;!&ndash;<el-tag size="mini" >{{item.branchName}}</el-tag>&ndash;&gt;-->
      <!--              <span>{{item.toolVersion}}</span>-->
      <!--            </el-col>-->
      <!--          </el-row>-->

      <!--          <el-row style="margin-top: 5px">-->
      <!--            <el-col :span="8">维护人：</el-col>-->
      <!--            <el-col :span="16">-->
      <!--              <span>{{item.tool.directorName}}</span>-->
      <!--            </el-col>-->
      <!--          </el-row>-->



      <!--        </el-col>-->
      <!--      </el-row>-->

      <!--      <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>-->
      <!--      <p class="description" @click="openViewBefore(item)" >{{item.tool.briefIntroduction}}</p>-->


      <!--      &lt;!&ndash; 固定在右下角的按钮组 &ndash;&gt;-->
      <!--      <div class="button-group">-->
      <!--        <el-button type="primary" size="mini" @click="openUpdBefore(item)"  >编辑</el-button>-->
      <!--        <el-button type="danger" size="mini" @click="delTips(item)"  >删除</el-button>-->
      <!--      </div>-->

      <!--    </div>-->
      <!--  </div>-->
      <!--</div>-->



      <!--分页部分-->
      <el-pagination
          style="width:33%; margin-right:10px; margin-top:10px;"
          background
          layout="total, prev, pager, next, jumper"
          @current-change="pageCurrentChange"
          :page-size="pageConfig.size"
          :current-page="pageConfig.current"
          :pager-count="5"
          :total="pageConfig.total">
      </el-pagination>

    </el-card>

    <OpsDeployToolSaveUpd  ref="OpsDeployToolSaveUpdRef" @getListPage="getListPage"/>
    <OpsDeployToolDetailed ref="OpsDeployToolDetailedRef"/>

  </div>

</template>


<script>

import http from '@/utils/http'
import OpsDeployToolSaveUpd  from './OpsDeployToolSaveUpd.vue'
import OpsDeployToolDetailed from './OpsDeployToolDetailed.vue'


export default {
  name: "OpsDeployToolList",
  props:{
    // 服务器id
    computerServerId:{
      type: String,
      required: false
    }
  },
  components: {
    OpsDeployToolSaveUpd,
    OpsDeployToolDetailed,
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        ports: null,
        deployUser: null,
        toolVersion: null,
        toolName: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 20,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
    }
  },
  methods: {
    /**
     * 渲染图片
     * 只能这样做，图片这里不支持非固定地址返回
     * @param item 目标数据
     */
    getImageUrlInfo(item) {
      if (!item || !item.ico || item.ico === 'null') {
        // 判空或无效，直接显示默认图
        this.$set(item, 'icoUrl', null);
        return;
      }

      http.getImageUrl(item.ico)
        .then(url => {
          // 异步赋值，触发响应式更新
          this.$set(item, 'icoUrl', url);
        })
        .catch(() => {
          // 下载失败 fallback 默认图
          this.$set(item, 'icoUrl', null);
        });
    },
    /**
     * 打开保存的弹框
     */
    openSaveBefore() {
      this.$refs.OpsDeployToolSaveUpdRef.init('save', null, this.computerServerId)
    },
    /**
     * 查看之前触发
     * @param obj 这一行数据
     */
    openViewBefore(obj){
      this.$refs.OpsDeployToolDetailedRef.init(obj)
    },
    /**
     * 打开修改之前的的弹出框
     * @param obj 修改的值
     */
    openUpdBefore(obj) {
      this.$refs.OpsDeployToolSaveUpdRef.init('upd', obj, this.computerServerId)
    },
    /**
     * 删除之前的提示框
     * @param obj 要删除的数据
     */
    delTips(obj) {
      this.$confirm('是否执行删除, 删除后无法恢复，是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      }).then(() => {
        // 确定删除
        this.delOpsDeployTool(obj)

      }).catch(() => {
        // 取消删除
      });
    },
    /**
     * 删除
     * @param obj 要删除的数据
     */
    delOpsDeployTool(obj) {
      http.post("opsDeployTool/delOpsDeployTool", {
        id: obj.id,
      }).then(response => {
        let res = response.data;
        this.getListPage()
        this.$message.success("删除成功");

      }).catch(error => {
        this.$message.error (error.message)
      })
    },
    /**
     * 分支分页查询列表
     */
    getListPage() {
      if (!this.computerServerId){
        return
      }
      this.queryParam.current = this.pageConfig.current
      this.queryParam.size = this.pageConfig.size
      this.queryParam.computerServerId = this.computerServerId
      http.post("/opsDeployTool/getListPage", this.queryParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records
        // 异步加载每个图片
        this.tableData.forEach(item => this.getImageUrlInfo(item));

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getListPage()
    },
     /**
     * 重置查询条件
     */
    resetQuery() {
      this.queryParam = {
        ports: null,
        deployUser: null,
        toolVersion: null,
        toolName: null,
      }
    },
     /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getListPage()
    },
  },
  watch:{
    // 计算机发生变化，这里进行刷新
    computerServerId(val){
      this.getListPage()
    },
  },
  // 进入马上执行
  mounted() {
    this.getListPage()
    this.$store.commit ('navigationListChange', [
      {name: '部署工具', url: '/toOpsDeployToolList', query: {}},
    ])
  },
}
</script>

<style scoped>


/**
====== 一个方框 start ===
 */

.container {
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  gap: 20px; /* 小框框之间的间距 */
  padding: 20px;
  min-height: 600px;
}

.component-ico {
  width: 126px;
  height: 110px;
}

.box {
  width: 380px; /* 固定宽度 */
  height: 270px; /* 固定高度 */
  min-height: 270px;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /*overflow: hidden;*/
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  position: relative; /* 添加这个以便version-tag可以绝对定位 */
  overflow: hidden; /* 保持hidden防止内容溢出 */
}

.box-content {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.box-content .description {
  margin-top: 0px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 显示3行 */
  -webkit-box-orient: vertical;
  cursor: pointer;
  margin-bottom: 6px;
}

.component-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  cursor: pointer;
}

/* 角标 start */


.env-type {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  clip-path: inset(0 -100%);
}

.env-type-demo {
  background: #FFE14C;
  box-shadow: 0 0 0 999px #FFE14C;
}

.env-type-dev {
  background: #FFA07A;
  box-shadow: 0 0 0 999px #FFA07A;
}

.env-type-prod {
  background: #FF6347;
  box-shadow: 0 0 0 999px #FF6347;
}

.env-type-test {
  background: #90EE90;
  box-shadow: 0 0 0 999px #90EE90;
}

.env-type-before{
  background: #FCE4EC;
  box-shadow: 0 0 0 999px #FCE4EC;
}

.env-type-other {
  background: #D3D3D3;
  box-shadow: 0 0 0 999px #D3D3D3;
}

/* 角标 end */

/* 按钮组固定在右下角 */
.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: auto; /* 关键属性，使按钮组固定在底部 */
  /*padding-top: 10px;*/
}

/**
====== 一个方框 end ===
 */

</style>

